/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "PIDControllerDesigner.h"
#include "ui_PIDControllerDesigner.h"

#include <QMessageBox>
#include <sstream>

using namespace std;
using namespace RafMat::SimInterface;

PIDControllerDesigner::PIDControllerDesigner(QWidget *parent) :
    ControllerDesigner(parent),
    ui(new Ui::PIDControllerDesigner)
{
    ui->setupUi(this);
}

PIDControllerDesigner::~PIDControllerDesigner()
{
    delete ui;
}

void PIDControllerDesigner::attachController(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_controllerConf = ctrl;

    try
    {
        ui->pidGain->setValue(QString::fromStdString(m_controllerConf->getParameter("gain")).toDouble());
        ui->pidB->setValue(QString::fromStdString(m_controllerConf->getParameter("b")).toDouble());
        ui->pidTi->setValue(QString::fromStdString(m_controllerConf->getParameter("ti")).toDouble());
        ui->pidTd->setValue(QString::fromStdString(m_controllerConf->getParameter("td")).toDouble());
        ui->pidN->setValue(QString::fromStdString(m_controllerConf->getParameter("n")).toDouble());
        ui->pidUBound->setValue(QString::fromStdString(m_controllerConf->getParameter("upper_bound")).toDouble());
        ui->pidLBound->setValue(QString::fromStdString(m_controllerConf->getParameter("lower_bound")).toDouble());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void PIDControllerDesigner::applySettings()
{
    try
    {
        ostringstream ostr;

        ostr << "[ ";
        ostr << ui->pidGain->value() << " ";
        ostr << ui->pidB->value() << " ";
        ostr << ui->pidTi->value() << " ";
        ostr << ui->pidTd->value() << " ";
        ostr << ui->pidN->value() << " ";
        ostr << ui->pidUBound->value() << " ";
        ostr << ui->pidLBound->value() << " ]";

        m_controllerConf->setParameter("*", ostr.str());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }

}




